import Vue from "vue";
import VueRouter from "vue-router";
import store from '../store/index'
const Home = () => import("../views/Home");

Vue.use(VueRouter);
/**
 * 静态路由
 */

const defaultRouter = [
  {
    path: "/Home",
    component: Home,
    name: "任务管理",
    // redirect: '/Home/DrTask',
    children: [   
      {
        path: "/taskManange/DrTask",
        component: () => import("../views/taskManange/DrTask.vue")
      },
      {
        path: "/fileReview/usedCarAudit",
        component: () => import("../views/fileReview/usedCarAudit.vue"),
        meta: { 
          keepAlive:true
         }
      },
     
      {
        path: "/taskManange/CrTask",
        component: () => import("../views/taskManange/CrTask.vue")
      },
      {
        path: "/task/censor",
        component: () => import("../views/task/censor.vue"),
        meta: { 
          keepAlive:true
         }
      },
      {
        path: "/uploadFile/uploadProduct",
        component: () => import("../views/uploadFile/uploadProduct.vue")
      },
      {
        path: "/uploadFile/iploadDitch",
        component: () => import("../views/uploadFile/iploadDitch.vue")
      }
    ]
  },
  {
    path: "/Home",
    component: Home,
    name: "",
    // redirect: '/DrStaffing',
    children: [  
      {
        path: "/blank/index",
        component: () =>
          import(
            '../views/blank/index.vue'
          )
      }
    ]
  },
  {
    path: "/Home",
    component: Home,
    name: "",
    children: [  
      {
        path: "/externalLink/index",
        component: () =>
          import(
            '../views/externalLinK/index.vue'
          )
      }
    ]
  },
  {
    path: "/Home",
    component: Home,
    name: "人员管理",
    // redirect: '/DrStaffing',
    children: [  
      {
        path: "/peopleManage/ConfigurationManage/articlereview/peopleManageAdd",
        component: () =>
          import(
            "../views/peopleManage/ConfigurationManage/articlereview/peopleManageAdd.vue"
          )
      },   
      {
        path: "/peopleManage/ConfigurationManage/creditaduit/peopleManageAdd",
        component: () =>
          import(
            "../views/peopleManage/ConfigurationManage/creditaduit/peopleManageAdd.vue"
          )
      },    
    ]
  },
  
  {
    path: "/Home",
    component: Home,
    name: "车辆类型",
    // redirect: '/DrTransfer',
    children: [
      {
        path: "/carType/formData",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/carType/formData.vue"
          )
      }
    ]
  }
];
/**
 * 动态路由添加
 */

const router = new VueRouter({
  base: process.env.NODE_ENV === "test" ? "" : "/pc_web/",
  routes:defaultRouter
});

// 避免路由守卫进入死循环
let OnRoute = true; //通过oneRun变量控制 避免陷入死循环
router.beforeEach((to,from,next)=>{
    if(OnRoute){
        // 首次进入Home存储token
        // console.log(to.path) /home
        // console.log(to.path.indexOf('carType'))
        // if(to.path.indexOf('carType')>0){
        //   document.title = '民生金融租赁车辆管理中心'
        // }else{
        //   document.title = '民生金融租赁审批中心'
        // }
        if(to.path=="/Home"&&to.query){
          localStorage.setItem("token", to.query.token);
          localStorage.setItem("MenuID", to.query.menuItemId);
          createNewRouter(to.query.menuItemId);
          next()
        }else{
          // 刷新之后进入
          createNewRouter();
          next({
            path: to.fullPath      
          })
          // next({...to, replace: true})// 必不可少的，确保你的动态路由创建成功再去执行其它代码
        }             
    }else{
      // 菜单切换 
      // console.log('菜单切换')
      // if(to.path!=="/Home"){
      //   // console.log('新')
      //   // localStorage.setItem("pathUrl", to.path);

      // }      
      next()
    }
})

function createNewRouter(val){
    //必须在creatNewRouter() 执行
    //请在这里做你登陆之后所做的创建动态路由的事情 一模一样去做。
    //不要用缓存，session或localstorage 否则会报错。你需要重新发出请求 登陆怎么做的 这里就怎么做的
    if(val){
      // 首次获取菜单
      store.dispatch("GetMenu",{menuId:val}).then(res => {     
      })       
    }else{
      // 刷新后获取菜单
      let id = localStorage.getItem("MenuID");
      store.dispatch("GetMenu",{menuId:id}).then(res => { 
      }) 
    }
    OnRoute = false;
};
export default router;
